//
//  LZCouponManagerListViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCouponManagerListViewController.h"
#import "LZCouponManagerListDetailViewController.h"
#import "UIViewController+NavigationItemSetting.h"
#import "LZNewPromotionVC.h"
#import "HTHorizontalSelectionList.h"
#import "LZCouponTableViewBaseView.h"
#import "Masonry.h"
#import "LZCouponPresenter.h"
#import "LZCouponListModel.h"
#import "MJExtension.h"
#import "LZUserModel.h"
#import "LZCreatePromotionViewController.h"
#import "LZCouponManagerGroupDetailViewController.h"
#import "LZCouponManagerCouponDetailViewController.h"

@interface LZCouponManagerListViewController ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource,LZCouponTableBaseViewDelegate>

@property (nonatomic, strong) HTHorizontalSelectionList * headerListView;
@property (nonatomic, strong) NSArray *carMakes;
@property (nonatomic, strong) LZCouponTableViewBaseView *tableBaseView;
@property (nonatomic, strong) LZCouponPresenter *couponPresenter;
@property (nonatomic, assign) CouponListTitleStateCode couponListTitleCode;
@property (nonatomic, assign) __block BOOL isFirstIn;
@end

@implementation LZCouponManagerListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.umLogPageViewName = @"优惠管理页面";
    
    self.couponListTitleCode = kCoupon_notSubmit;
    
    self.isFirstIn = YES;
    
    _couponPresenter = [[LZCouponPresenter alloc] init];
    
    [self configureNavBar];
    
    [self configureUI];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self getCouponStateCountData];
}

#pragma mark -
#pragma mark private

- (void)creatCouponBtnAction
{
    [self.tableBaseView tableViewNeedStopVideoWithCouponListCode:self.couponListTitleCode];
    
    LZNewPromotionVC * vc = [[LZNewPromotionVC alloc] initWithNibName:@"LZNewPromotionVC" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getCouponStateCountData
{
    WS(wSelf);
    [self.couponPresenter getCouponStateCountWithSellerID:[LZUserModel Instance].sellerId ResultSuccess:^(id data) {
        
        int notSub = [data[@"hadSaved"] intValue];
        int wait = [data[@"reviewPass"] intValue];
        int published = [data[@"hadPublished"] intValue];
        int finished = [data[@"hadOffline"] intValue];
        
        wSelf.carMakes = @[[NSString stringWithFormat:@"%@ (%d)",LZLocalizedString(@"not_submitted", nil),notSub],
                           [NSString stringWithFormat:@"%@ (%d)",LZLocalizedString(@"waiting_for_publishing", nil),wait],
                           [NSString stringWithFormat:@"%@ (%d)",LZLocalizedString(@"published", nil),published],
                           [NSString stringWithFormat:@"%@ (%d)",LZLocalizedString(@"finished", nil),finished]
                           ];
        [wSelf.headerListView reloadData];
        if (!wSelf.headerListView.isClickButton) {
            [wSelf.headerListView setNeedsDisplay];
        }
        if (wSelf.isFirstIn) {
             [wSelf.tableBaseView clickTitleBtnWithTag:kCoupon_notSubmit withSellerId:[LZUserModel Instance].sellerId];
            wSelf.isFirstIn = NO;
        }
        
    } Failure:^(NSString *fail) {
        
    }];
}

#pragma mark -
#pragma mark configureUI
- (void)configureUI
{
    self.title = LZLocalizedString(@"home_menu_manage_coupons", nil);
    [self.view addSubview:self.headerListView];
    [self.view addSubview:self.tableBaseView];
    
    [self.headerListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(55);
    }];
    
    [self.tableBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.top.equalTo(self.headerListView.mas_bottom);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)configureNavBar
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 40, 40);
    UIImage *addImage = [UIImage imageNamed:@"add.png"];
    [btn setImage:addImage forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(creatCouponBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self navigationItemSetting:@[btn] type:NAVIGATIONITEMSETTING_RIGHT];
}

#pragma mark - 
#pragma mark 头部选择控件协议方法
- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList {
    return self.carMakes.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index {
    return self.carMakes[index];
}

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index {
    // update the view for the corresponding index
    
    DLog(@"select : %ld", (long)index);
    switch (index) {
        case 0:
            self.couponListTitleCode = kCoupon_notSubmit;
            break;
        case 1:
            self.couponListTitleCode = kCoupon_waitPublish;
            break;
        case 2:
            self.couponListTitleCode = kCoupon_hasBeenPublished;
            break;
        case 3:
            self.couponListTitleCode = kCoupon_hasBeenFinished;
            break;
            
        default:
            break;
    }
    [self.tableBaseView clickTitleBtnWithTag:self.couponListTitleCode withSellerId:[LZUserModel Instance].sellerId];
}


#pragma mark -
#pragma mark couponTableview BaseScrollView delegate
- (void)tableViewCellClickBtnWithModel:(LZCouponListModel *)dataModel couponEventCode:(CouponListEventCode)eventCode couponListStateCode:(CouponListTitleStateCode)couponListStateCode operationSuccess:(void (^)(CouponListEventCode, LZCouponListModel *))successBlock
{
    if (eventCode == kCouponList_detail) {
        
        LZCouponManagerListDetailViewController *vc = [[LZCouponManagerListDetailViewController alloc] init];
        vc.couponListModel = dataModel;
        [vc setOperationCouponStateSuccessBlock:^(CouponListEventCode eventCode, LZCouponListModel *returnModel) {
            if (successBlock) {
                successBlock(eventCode,returnModel);
            }
        }];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (eventCode == kCouponList_edit){
        
        LZCreatePromotionViewController *vc = [[LZCreatePromotionViewController alloc] init];
        vc.couponListModel = [dataModel copy];
        vc.eventCode = kCouponList_edit;
        [vc setOperationCouponStateSuccessBlock:^(CouponListEventCode eventCode, LZCouponListModel *returnModel) {
            if (successBlock) {
                successBlock(eventCode, returnModel);
            }
        }];
        [self.navigationController pushViewController:vc animated:YES];
    
    }else if (eventCode == kCouponList_copy){
        
        LZCreatePromotionViewController *vc = [[LZCreatePromotionViewController alloc] init];
        vc.couponListModel = [dataModel copy];
        vc.eventCode = kCouponList_copy;
        [vc setOperationCouponStateSuccessBlock:^(CouponListEventCode eventCode, LZCouponListModel *returnModel) {
            if (successBlock) {
                successBlock(eventCode, nil);
            }
        }];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (eventCode == kCouponList_groupDetail){
        
        LZCouponManagerGroupDetailViewController *vc = [[LZCouponManagerGroupDetailViewController alloc] init];
        vc.dataModel = dataModel;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (eventCode == kCouponList_couponDetail){
        
        LZCouponManagerCouponDetailViewController *vc = [[LZCouponManagerCouponDetailViewController alloc] init];
        vc.dataModel = dataModel;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


/**
 子列表操作成功后.同步刷新head的数字
 */
- (void)tableViewCellClickBtnSuccessToRefreshSupeViewTitleCount
{
    [self getCouponStateCountData];
}

#pragma mark -
#pragma mark 初始化
- (HTHorizontalSelectionList *)headerListView {
    
    if (_headerListView == nil) {
        _headerListView = [[HTHorizontalSelectionList alloc] init];
        [_headerListView setScrollViewColor:UIColorFromRGB(0xffffff, 1)];
        _headerListView.delegate = self;
        _headerListView.dataSource = self;
    }
    
    return _headerListView;
}

- (LZCouponTableViewBaseView *)tableBaseView
{
    if (!_tableBaseView) {
        _tableBaseView = [[LZCouponTableViewBaseView alloc] initWithFrame:CGRectZero];
        _tableBaseView.couponTableBaseViewDelegate = self;
    }
    
    return _tableBaseView;
}

@end
